﻿using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Data;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week3.WHAllot_CRUD
{
    /// <summary>
    /// 添加仓库调拨单
    /// </summary>
    public class AddAllottDataHander : IRequestHandler<AddAllottDataCommand, Result>
    {
        private IBaseRepository<dynamic> dynamicRepository;

        public AddAllottDataHander(IBaseRepository<dynamic> dynamicRepository)
        {
            this.dynamicRepository = dynamicRepository;
        }

        public async Task<Result> Handle(AddAllottDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            List<string> SqlList = new List<string>();
            WHAllot wHAllot = new WHAllot()
            {
                WHAllotId = request.WHAllotId,
                WHAllotNum = request.WHAllotNum,
                WHAllotTime = request.WHAllotTime,
                WarehouseSourceId = request.WarehouseSourceId,
                WarehouseSourceAddr = request.WarehouseSourceAddr,
                WarehouseGoalId = request.WarehouseGoalId,
                WarehouseGoalAddr = request.WarehouseGoalAddr,
                WHAllotState = request.WHAllotState,
                WHAllotRemark = request.WHAllotRemark,
                WHAllotCreateName = request.WHAllotCreateName,
                WHAllotCreateBranch = request.WHAllotCreateBranch
            };
            SqlList.Add($"insert into WHAllot values({request.WHAllotId},'{request.WHAllotTime.ToString("yyyy-MM-dd")}',{request.WarehouseSourceId},'{request.WarehouseSourceAddr}',{request.WarehouseGoalId},'{request.WarehouseGoalAddr}',{request.WHAllotState},'{request.WHAllotRemark}','{request.WHAllotCreateName}','{request.WHAllotCreateBranch}','{request.WHAllotNum}');");
            foreach (var item in request.allotDatas)
            {
                AllotData allotData = new AllotData()
                {
                    AllotDataId = item.AllotDataId,
                    WHAllotNum = item.WHAllotNum,
                    SuppliesNumber = item.SuppliesNumber,
                    SuppliesName = item.SuppliesName,
                    Specification = item.Specification,
                    Monad = item.Monad,
                    AllotDataNum = item.AllotDataNum,
                    Batch_number = item.Batch_number,
                    ValidityTime = item.ValidityTime,
                    CreateTime = item.CreateTime,
                    OverTime = item.OverTime,
                    Digest = item.Digest,
                    IsAllotDataDel = item.IsAllotDataDel,
                };
                if (item.CreateTime != null && item.OverTime != null && item.ValidityTime != null)
                {
                    SqlList.Add($"insert into AllotData values(0,'{item.WHAllotNum}','{item.SuppliesNumber}','{item.SuppliesName}','{item.Specification}','{item.Monad}',{item.AllotDataNum},'{item.Batch_number}','{item.ValidityTime}','{item.CreateTime}','{item.OverTime}','{item.Digest}',{item.IsAllotDataDel});");
                }
               
                SqlList.Add($"update Storage set InventoryInfoNum=InventoryInfoNum-{item.AllotDataNum} where StorageId={item.StorAgeId};");
            }
            var AddNum=dynamicRepository.AffairCRUD(SqlList);
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
